package service;

import com.alibaba.fastjson.JSON;
import model.ViewForRelation;
import pond.common.STRING;
import pond.db.DB;
import pond.web.Render;
import view.ReportView;

import java.util.List;

/**
 * Created by sky_wang on 2017/7/19.
 */
public class RelationService {
    private DB db;
    public RelationService(DB db){
        this.db = db;
    }
    public boolean isExist(String id)
    {
        List<ViewForRelation> list = this.db.get(t -> t.query(ViewForRelation.class, "select * from view_for_relation where vid = '"+id+"';"));
        if (list !=null && list.size()>0)
        {
            return true;
        }
        return false;
    }
    public ViewForRelation get(String id)
    {
        List<ViewForRelation> list = this.db.get(t -> t.query(ViewForRelation.class, "select * from view_for_relation where vid = '"+id+"';"));
        if (list !=null && list.size()>0)
        {
            return list.get(0);
        }
        return null;
    }
    public boolean add(ViewForRelation vfr)
    {
        if (isExist(vfr.get("vid")))
        {
            return false;
        }
        else
        {
            this.db.post(t -> {
                t.recordInsert(vfr);
            });
        }
        return true;
    }
    public boolean update(ViewForRelation vfr)
    {
        if (isExist(vfr.get("vid")))
        {
            this.db.post(t -> {
                t.recordUpdate(vfr);
            });
        }
        return true;
    }
}
